$(function(){ 
    $("select").css({ visibility: "hidden" }); 
	$('.table-sort').dataTable(pageStting(baseURL));
	$('#DataTables_Table_0_length').find('label').contents().wrap("<span class='f-l mr-10'></span>");
	$(window).resize(function(){
		$(".table-sort").css('width','100%');
	})
	 if(isManagerPlat=='false'){
			$('#orgId').parent().parent().hide();
	 }else{
//		 $.getJSON(baseURL+'/api/organization/listAll',function(result){
//			 $.each(result.results,function(i,n){
//				 $('#orgId').append("<option value=''>全部</option>");
//				 $('#orgId').append("<option value='"+n.id+"'>"+n.name+"</option>");
//			 })
//		 })
//		 $('#orgId').next('span').find('input').attr('placeholder','请选择组织'); 
			$.ajax({
				type:"GET",
				contentType : "application/x-www-form-urlencoded; charset=UTF-8", 
				url:baseURL + '/api/organization/listAll',
				dataType:"JSON",
				success:function(result){
					if(result.flag){
						if(result.results.length!=0){
							$(".pagitionList").empty();
							$.each(result.results,function(i,n){
								$(".pagitionList").append("<li value='"+n.id+"'>"+n.name+"</li>")
							})
							$(".pagitionList>li").on('click',function(e){
								e.stopPropagation();
								$("#orgId").val($(e.target).text());
								localStorage.uOrgId=$(e.target).attr("value");
								$(".pagitionList>li").removeClass("airportSelectBK");
								$(e.target).addClass("airportSelectBK");
							})
						}else {
							$(".pagitionList").empty().append("<li>没有数据</li>")
						} 
					}else layer.msg("数据报错："+result.message); 
				}
			  })
			  var isMouse=true;
			  $("#orgId").on('click',function(){
				  $('.pagitionDiv').toggle();
			  })
			$('.pagitionDiv').on('mouseleave',function(e){
				$('.pagitionDiv').hide();
				isMouse=true;
			})
			$('.pagitionDiv').on('mouseover',function(e){
				isMouse=false;
			})
			$('#orgId').on('blur',function(e){
				if(isMouse){
					$(e.target).next().hide();
				}
			})
	 }

});

// 从服务器分批取数据
 pageStting =  function(baseURL){
		var optsortable = {
					"bPaginate":true,
					"bProcessing":true,
					"stateSave":true,
					"iDisplayLength":10,
					"sAjaxSource":baseURL+'/api/employee/list',
					"sPaginationType":'full_numbers',
					"bServerSide": true,
					"bSort": false,             
					"bFilter": false,  
				     "aoColumnDefs": [                 
				                          {               
	                            "render": function (data, type, full){  
	                                return '<input type="checkbox" name="SelectedUserId"  class="btn btn-danger radius" value="' + full[0]+ '"/>';   
	                              },
	                            "targets": 0 
	                           },
	                          {               
		                            "render": function (data, type, full){  
		                            	var edit_herf ="<div class='text-c handlePointer'><img title='编辑用户' alt='编辑用户' src='"+baseURL+"/images/admin/cp_edite.png' onclick=layer_show('850','','编辑用户','"+baseURL+"/api/employee/page/"+full[0]+"') />"
		                            	var reset_password_herf ="<img title='重置密码' alt='重置密码' src='"+baseURL+"/images/admin/cp_password.png' onclick=layer_show('600','','重置密码','"+baseURL+"/api/employee/resetpassword/"+full[8]+"') />"
		    							var trach_herf="<img title='删除' alt='删除' src='"+baseURL+"/images/admin/cp_delete.png' onclick=\"return deleteData(null,'"+full[0]+"')\" /></div>"
		                            	return edit_herf+reset_password_herf+trach_herf;
		                              },
		                            "targets":7
		                           }
				                   ] ,
					"fnServerData": function(sSource, aoData, fnCallback){
							var globalSearch = {};
							var searchConditions = {};
							if($.trim($("#userName").val())!=''){
								searchConditions.userName=$.trim($("#userName").val());
							}
							if($.trim($("#orgId").val())!=''&&$.trim($("#orgId")!=undefined)){
							  searchConditions.orgId=localStorage.uOrgId; 
							}
							globalSearch.name= 'globalSearch';
							globalSearch.value= JSON.stringify(searchConditions);
							aoData.push(globalSearch);
							 $.ajax({  
						        type: "GET",     
						        url: sSource,     
						        dataType: "json",    
						        contentType: "application/json;charset=UTF-8", 
						        data: aoData, // 以json格式传递
						        success: function(result) {
					    			if(!result.flag){
					            		if(result.statusCode==503){
					            		  location=baseURL+"/api/tologin";
					            		}else{
					            		  layer.msg("查询失败！");
					            		}
					            	  return;
					            	}
						        	if(result.flag){
							        	var obj =result;
										var data = obj.results;
										var aaData = [];
										var dataResult = {};
										dataResult.iTotalRecords = obj.totalcount;
										dataResult.iTotalDisplayRecords= obj.totalcount;
										dataResult.iDisplayStart = obj.offset;
										dataResult.iDisplayLength= obj.limit;
										dataResult.aaData = aaData;
										// json重构数组对象
										$.each(data,function(i,val){
											aaData[i]=[];
											aaData[i].push(val.id);
											aaData[i].push(val.loginName==null?'/':val.loginName);
											aaData[i].push(val.empName==null?'/':val.empName);
											aaData[i].push(val.code==null?'/':val.code);
											aaData[i].push(val.orgName==null?'/':val.orgName);
											aaData[i].push(val.mobilePhone==null?'/':val.mobilePhone);
											aaData[i].push(val.roleName==null?'/':val.roleName);
											aaData[i].push(val.null_n);
											aaData[i].push(val.userId);
										});
							            fnCallback(dataResult);   
						          }
						        }    
						     });
					}
				}	
		return optsortable;
}
 		deleteData = function(selName,id){
							var ids= "";
							if(selName==null){
								ids = id;
							}else{
								var objs = $('input[name='+selName+']');
								var size = objs.length;
								$.each(objs,function(i,val){
									if(objs[i].checked) {
										if(objs[i].value!=""){
											if(ids==""){
												ids = objs[i].value;
											}else{
											  ids = ids+","+objs[i].value;
											}
										}
									}
								});
							}
							if(ids==""){
								layer.msg("请选择删除项！");
								return false;
							}
							var data = {};
							data.ids=ids;
							layer.confirm("您确定要删除吗？",function(){
								$.ajax({  
							        type: "post",     
							        url: '../delete',     
							        dataType: "json",   
							        contentType: "application/json",
							        data: JSON.stringify(data),
							        success: function(result) {
							        	   if(!result.flag){
							        		   layer.msg("删除失败！原因："+result.message);
							        	   }else{
							        		   location.reload();
							        	   }
							        }    
							     });
							})
							return true;
							}
					
doSearch = function(){
	   var seachValue={};
	   seachValue.orgId =localStorage.uOrgId;
	   $('.table-sort').DataTable().search(seachValue).draw();
}	 
//$("select").bind("click",function(){
//	$("select").css({ visibility: "visible" }); 
//});